package com.ztgf.order.api.cms;

import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.utils.CustomizeUtil;
import com.ztgf.common.utils.ExcelExport2;
import com.ztgf.order.models.bo.OrderShopStatisticsBO;
import com.ztgf.order.models.bo.OrderStatisticsBO;
import com.ztgf.order.models.service.IOrderStatisticsService;
import com.ztgf.order.models.vo.OrderVariousItemsVO;
import com.ztgf.order.models.vo.OrderVariousShopItemsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * author: chenyu.wang
 * date: 2020-07-21 14:54
 * Description: 订单统计控制器
 */
@RestController
@RequestMapping("/cms/orderStatisticsController")
@Api(value = "OrderStatisticsController", tags = "后台管理系统订单统计接口")
public class OrderStatisticsController {

    @DubboReference
    private IOrderStatisticsService orderStatisticsService;

    @PostMapping(value = "/queryOrderStatisticsToDay")
    @ApiOperation(value = "后台管理系统订单统计今日订单总量", notes = "后台管理系统订单统计今日订单总量")
    public Result queryOrderStatisticsToDay(OrderStatisticsBO bo) {
        Integer num = orderStatisticsService.queryOrderStatisticsToDay(bo);
        return new Result<>(num);
    }

    @PostMapping(value = "/queryOrderVariousItems")
    @ApiOperation(value = "各类目整体订单统计", notes = "各类目整体订单统计")
    public Result queryOrderVariousItems(OrderStatisticsBO bo) {
//        Page<OrderVariousItemsVO> list = orderStatisticsService.queryOrderVariousItems(bo);
        Page<OrderVariousItemsVO> page = orderStatisticsService.queryNewCategoryOrderCount(bo);
        return new Result<>(page);
    }

    @PostMapping(value = "/queryOrderVariousShopItems")
    @ApiOperation(value = "各类目店铺订单统计", notes = "各类目店铺订单统计")
    public Result queryOrderVariousShopItems(OrderShopStatisticsBO bo) {
        Page<OrderVariousShopItemsVO> page = orderStatisticsService.queryOrderVariousShopItems(bo);
        return new Result<>(page);
    }

    @GetMapping(value = "/exportOrderVariousItems")
    @ApiOperation(value = "各类目整体订单导出", notes = "各类目整体订单导出")
    public void exportOrderVariousItems(HttpServletResponse response, String startTime, String endTime) {
        //创建一个数组用于设置表头
        String[] attributeNames = new String[]{"编号", "类目名称", "订单数量"};
        //查询数据
        OrderStatisticsBO bo = new OrderStatisticsBO();
        bo.setPageNo(1);
        bo.setPageSize(1000);
        if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
            bo.setStartTime(startTime);
            bo.setEndTime(endTime);
        }
        Page<OrderVariousItemsVO> page = orderStatisticsService.queryNewCategoryOrderCount(bo);
        List<OrderVariousItemsVO> list = page.getRecords();
        if (CustomizeUtil.listIsNull(list)){
            OrderVariousShopItemsVO orderVariousShopItemsVO = new OrderVariousShopItemsVO();
            orderVariousShopItemsVO.setCategoryName("暂无信息");
            orderVariousShopItemsVO.setShopName("暂无信息");
        }
        ExcelExport2.export(response, list, attributeNames, "_各类目整体订单");
    }

    @GetMapping(value = "/exportOrderVariousShopItems")
    @ApiOperation(value = "各类目店铺订单统计", notes = "各类目店铺订单统计")
    public void OrderVariousShopItems(HttpServletResponse response, String startTime, String endTime) {
        //创建一个数组用于设置表头
        String[] attributeNames = new String[]{"编号", "类目编号", "店铺名称", "类目名称", "订单量"};
        //查询数据
        OrderShopStatisticsBO bo = new OrderShopStatisticsBO();
        bo.setPageNo(1);
        bo.setPageSize(1000);
        if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
            bo.setStartTime(startTime);
            bo.setEndTime(endTime);
        }
        Page<OrderVariousShopItemsVO> page = orderStatisticsService.queryOrderVariousShopItems(bo);
        List<OrderVariousShopItemsVO> list = page.getRecords();
        if (CustomizeUtil.listIsNull(list)){
            OrderVariousShopItemsVO orderVariousShopItemsVO = new OrderVariousShopItemsVO();
            orderVariousShopItemsVO.setCategoryName("暂无信息");
            orderVariousShopItemsVO.setShopName("暂无信息");
        }
        ExcelExport2.export(response, list, attributeNames, "_各类目店铺订单");
    }
}
